package com.ekalenchuk.module.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ekalenchuk.module.domain.Article;

@Repository
public class ArticleDaoImpl implements Serializable, ArticleDAO {

	private static final long serialVersionUID = 4775340893949003282L;

	@Autowired
	private SessionFactory sessionFactory;

	public Article getArticle(Long articleId) {
		Article article = (Article) sessionFactory.getCurrentSession().load(
				Article.class, articleId);
		return article;
	}

	public void save(Article article) {
		sessionFactory.getCurrentSession().save(article);
	}

	@SuppressWarnings("unchecked")
	public List<Article> getArticles() {
		List<Article> articles = (List<Article>) sessionFactory
				.getCurrentSession().createCriteria(Article.class).list();
		return articles;
	}

	public void removeArticle(Long articleId) {
		Article article = getArticle(articleId);
		if (null != article) {
			sessionFactory.getCurrentSession().delete(article);
		}

	}

}
